<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<table id="role-datagrid" class="easyui-datagrid"
       data-options="toolbar: '#role-datagrid-toolbar',url:'/role.html?act=list',singleSelect:true,pagination:true">
    <thead>
    <tr>
        <th data-options="field:'id',width:130">ID</th>
        <th data-options="field:'name',width:130">角色名</th>
        <th data-options="field:'remark',width:130">备注</th>
    </tr>
    </thead>
</table>
<div id="role-datagrid-toolbar" style="height:auto">
    <div style="padding:2px 5px;">
        id: <input id="search-role-id"/>
        名称: <input id="search-role-name">
        <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="doRoleSearch()">搜索</a>
    </div>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'"
       onclick="doRoleEdit(0)">添加</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
       onclick="doRoleEdit(1)">编辑</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'"
       onclick="doRoleAssign()">分配权限</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"
       onclick="doRoleDel()">删除</a>
</div>

<div id="edit-role-dialog" class="easyui-dialog" modal="true" title="编辑角色" closed="true"
     href="<%=request.getContextPath()%>/role_edit.jsp" style="width:300px;height:200px;padding-top:30px"
     buttons="#dlg-role-buttons">
</div>
<div id="dlg-role-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitRoleForm()">提交</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#edit-role-form').form('reset')">重填</a>
</div>

<div id="role-menu-dialog" class="easyui-dialog" modal="true" title="分配菜单" style="width: 300px;"
     closed="true" data-options="buttons: '#dlg-role-menu-buttons'">
    <ul id="menu-role-list" class="easyui-tree"></ul>
</div>
<div id="dlg-role-menu-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:assignMenu()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#role-menu-dialog').dialog('close')">取消</a>
</div>
<script type="text/javascript">

    function doRoleSearch() {
        var queryParams = $('#role-datagrid').datagrid('options').queryParams;
        queryParams.id = $("#search-role-id").val();
        queryParams.name = $("#search-role-name").val();
        $("#role-datagrid").datagrid('reload');
    }

    function submitRoleForm() {
        $('#edit-role-form').form('submit', {
            onSubmit: function () {
                if ($(this).form('enableValidation').form('validate')) {
                    $.ajax({
                        url: "/role.html?act=edit",
                        data: $('#edit-role-form').serialize(),
                        method: "post",
                        success: function (data) {
                            if (data.status) {
                                $("#edit-role-dialog").dialog("close");
                                $('#edit-role-form').form('clear');
                                $('#role-datagrid').datagrid('reload');
                            } else {
                                alert(data.message);
                            }
                        }
                    })
                }
                return false;
            }
        });
    }

    function doRoleEdit(type) {
        if (type == 0) {
            $("#edit-role-dialog").dialog("open");
            $('#edit-role-form').form('clear');
        } else {
            var row = $('#role-datagrid').datagrid('getSelected');
            $("#edit-role-dialog").dialog({
                onLoad: function () {
                    $('#edit-role-form').form('clear');
                    $('#edit-role-form').form('load', {
                        name: row.name,
                        id: row.id,
                        remark: row.remark
                    });
                }
            })
            $("#edit-role-dialog").dialog("open");
        }

    }


    function doRoleDel() {
        var row = $('#role-datagrid').datagrid('getSelected');
        if (row != null && row != "") {
            if (!confirm("确定要删除吗？")) {
                return;
            }
            $.ajax({
                url: '/role.html?act=delete',
                type: 'POST',
                data: {id: row.id},
                success: function (data) {
                    if (data.status) {
                        $('#role-datagrid').datagrid('reload');
                    } else {
                        alert(data.message);
                    }
                }
            });
        }
    }

    var menuTree;

    function doRoleAssign() {
        var row = $('#role-datagrid').datagrid('getSelected');
        if (row != null && row != "") {
            //加载所有菜单
            var resultData = new Array();
            $.ajax({
                url: '/role.html?act=role_menu',
                type: 'POST',
                data: {roleId: row.id},
                dataType: "json",
                cache: false,
                success: function (json) {
                    resultData = json;
                    //加载菜单树列表
                    menuTree = $("#menu-role-list").tree({
                        url: '/role.html?act=menu_list',
                        method: "post",
                        checkbox: true,
                        onLoadSuccess: function (node, data) {
                            //点击选中角色已有菜单
                            for (var i = 0; i < resultData.length; i++) {
                                var nodeMenu = menuTree.tree("getChildren", menuTree.tree("find", resultData[i]).target);
                                if (nodeMenu.length < 1) {
                                    menuTree.tree("check", menuTree.tree("find", resultData[i]).target);
                                }
                            }
                            var defNode = menuTree.tree("find", 1);
                            menuTree.tree('remove', defNode.target);
                            $("#role-menu-dialog").dialog("open");
                        }
                    });
                }
            });
        }
    }

    function assignMenu() {
        var row = $('#role-datagrid').datagrid('getSelected');
        var menuIds = new Array();
        $(menuTree.tree("getChecked")).each(function (i, node) {
            menuIds[i] = node["id"];
        });
        var index = menuIds.length;
        $(menuTree.tree('getChecked', 'indeterminate')).each(function (i, node) {//半选中状态
            menuIds[i + index] = node["id"];
        });
        $.ajax({
            url: '/role.html?act=assign_menu',
            type: 'POST',
            data: {menuIds: menuIds.join(","), roleId: row["id"]},
            cache: false,
            success: function (data) {
                if (data.status)  {
                    $("#role-menu-dialog").dialog("close");
                } else {
                    alert(data.message);
                }
            }
        });
        return false;
    }

</script>
